#!/usr/bin/env bash

help(){
    cat<<__EOF__
    usage: m dock [ showdelay | autohide | magnification | position | addblankspace | addrecentitems | prune | help ]

    Examples:
      m dock showdelay x.x          # Changes how long the Dock takes to show up when auto-hide is enabled
      m dock autohide YES           # Enable Dock's auto hide feature
      m dock autohide NO            # Disable Dock's auto hide feature
      m dock magnification YES      # Turn magnification on
      m dock magnification NO       # Turn magnification off
      m dock position BOTTOM        # Change Dock's position to the bottom of the screen
      m dock position LEFT          # Change Dock's position to the left of the screen
      m dock position RIGHT         # Change Dock's position to the right of the screen
      m dock addblankspace          # Add a blank space (separator) to the Dock
      m dock addrecentitems         # Add a stack containg your recent items to the Dock
                                    #  (You can change the stack's type by right clicking on it)
      m dock prune                  # remove all items from dock
__EOF__
}

show_delay(){
    case $1 in
        [0-9][.][0-9])
            echo "New Auto-Hide time: "$1
            defaults write com.apple.dock autohide-time-modifier -float $1
            ;;
        [0-9])
            echo "New Auto-Hide time: "$1
            defaults write com.apple.dock autohide-time-modifier -int $1
            ;;
        *)
            echo "Current Auto-Hide time: $(defaults read com.apple.dock autohide-time-modifier 2>/dev/null)"
            exit 0
            ;;
    esac
    killall Dock
}

auto_hide(){
    case $1 in
        [yY][eE][sS])
            echo "Auto Hide: YES"
            defaults write com.apple.dock autohide -boolean YES
            ;;
        [nN][oO])
            echo "Auto Hide: No"
            defaults write com.apple.dock autohide -boolean NO
            ;;
        *)
            echo "Auto Hide: $(defaults read com.apple.dock autohide)"
            exit 0
            ;;
    esac
    killall Dock
}

magnify(){
    case $1 in
        [yY][eE][sS])
            echo "Magnification: YES"
            defaults write com.apple.dock magnification -boolean YES
            ;;
        [nN][oO])
            echo "Magnification: NO"
            defaults write com.apple.dock magnification -boolean NO
            ;;
        *)
            echo "Magnification: $(defaults read com.apple.dock magnification)"
            exit 0
            ;;
    esac
    killall Dock
}

dock_position(){
    case $1 in
        [bB][oO][tT][tT][oO][mM])
            echo "Dock Position: BOTTOM"
            defaults write com.apple.dock orientation bottom
            ;;
        [lL][eE][fF][tT])
            echo "Dock Position: LEFT"
            defaults write com.apple.dock orientation left
            ;;
        [rR][iI][gG][hH][tT])
            echo "Dock Position: RIGHT"
            defaults write com.apple.dock orientation right
            ;;
        *)
            echo "Position: $(defaults read com.apple.dock orientation)"
            exit 0
            ;;
    esac
    killall Dock
}

add_blank_space(){
    echo "New blank space added to the Dock"
    defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
    killall Dock
}

add_recent_items(){
    echo "Add recent items stack"
    defaults write com.apple.dock persistent-others -array-add '{"tile-data" = {"list-type" = 1;}; "tile-type" = "recents-tile";}';
    killall Dock
}

prune(){
    echo "remove all items from stack"
    defaults write com.apple.dock persistent-apps '()'
    killall Dock
}

case $1 in
    help)
        help
        ;;
    showdelay)
        shift
        show_delay $@
        ;;
    autohide)
        shift
        auto_hide $@
        ;;
    magnification)
        shift
        magnify $@
        ;;
    position)
        shift
        dock_position $@
        ;;
    addblankspace)
        shift
        add_blank_space
        ;;
    addrecentitems)
        shift
        add_recent_items
        ;;
    prune)
        shift
        prune
        ;;
    *)
        help
        ;;
esac

# vim: ts=4 sw=4 softtabstop=4 expandtab
